<?php
namespace App\Http\Controllers\Passenger\V2\User;

use App\Enum\ActivityEnum;
use App\Http\Controllers\Controller;
use App\Http\Requests\PassengerUserRequest;
use App\Models\UserCoupon;
use App\Services\PassengerActivityService;
use App\Services\PassengerLoginService;
use App\Services\PassengerUserActivityService;
use App\Services\PassengerUserCouponService;
use App\Services\PassengerUserService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


/**
 * 用户信息相关
 * Class UserController
 * @package App\Http\Controllers\Passeneger\User
 */
class UserController extends Controller
{


    /**
     * 初始化 用户信息
     */
    public function setInit(PassengerUserRequest $request, PassengerUserService $service)
    {

        $operation_field = [
            //头像， 昵称， 密码， 确认密码
            'avatar', 'nick_name', 'password', 'confirm_password', 'code'
        ];

       $save_data = $request->all($operation_field);
       unset($save_data['confirm_password']);
       if( Auth::user()->status == 3 )
           $save_data['status'] = 1;

       //修改信息
       $user_model = $service->saveByUserId(Auth::id(), $save_data);
       if( !$user_model )
           return $this->sendError('操作失败');

       //更新token
        $login_service = new PassengerLoginService();
        $login_info    = $login_service->respondWithToken($user_model);
        return $this->sendJson($login_info);
    }


    /**
     * 设置用户 Unionid
     * @param Request $request
     * @param PassengerUserService $service
     */
    public function setUnionid(Request $request, PassengerUserService $service)
    {
        $code = $request->post('code');
        if($code)
            $this->sendError('缺少参数');

        return $this->sendJson([
            'status'=> $service->setUnionid($code)
        ]);
    }




    /**
     * 小程序授权
     */
    public function authWxmini(Request $request, PassengerUserService $service)
    {
        $code = $request->post('code');

        return $this->sendJson($service->authWxmini($code));

    }



    /**
     * 获取用户详细信息
     * @param PassengerUserService $service
     * @return UserController|\Illuminate\Http\JsonResponse
     */
    public function detail(PassengerUserService $service)
    {
        return $this->sendJson($service->getDetail(Auth::id()));
    }


    /**
     * 信息修改
     * @param PassengerUserRequest $request
     * @param PassengerUserService $service
     * @return UserController|\Illuminate\Http\JsonResponse
     */
    public function edit(PassengerUserRequest $request, PassengerUserService $service)
    {
        $operation_field = [
            //头像， 昵称，
            'avatar', 'nick_name'
        ];

        $save_data = array_filter($request->all($operation_field));

        $user_model = $service->saveByUserId(Auth::id(), $save_data);
        if( !$user_model )
            return $this->sendError('操作失败');

        return $this->sendSuccess();
    }


    /**
     * 修改密码
     * @param PassengerUserRequest $request
     * @param PassengerUserService $service
     * @return UserController|\Illuminate\Http\JsonResponse
     */
    public function setPassword(PassengerUserRequest $request, PassengerUserService $service)
    {
        $operation_field = [
            //原密码， 新密码， 确认密码
            'old_password', 'new_password', 'confirm_password'
        ];

        $password_data = array_filter($request->all($operation_field));

        $user_model = $service->saveByUserId(Auth::id(), [ 'password'=> $password_data['new_password']]);
        if( !$user_model )
            return $this->sendError('操作失败');

        return $this->sendSuccess();
    }


    /**
     * 我的优惠券列表
     */
    public function getCouponList(Request $request, PassengerUserCouponService $service)
    {
        $status = $request->get('status', 1);

        $list = $service->getListAll(Auth::id(), $status);

        return $this->sendJson($list);

    }


    /**
     * 领取我的优惠券
     */
    public function drawCoupon(Request $request, PassengerUserCouponService $service)
    {

        $id = $request->post('id');

        $user_coupon_info = UserCoupon::query()->where('user_id', Auth::id())->find($id);

        if(!$user_coupon_info)
            return $this->sendError('优惠券不存在');

        if($user_coupon_info->status != 0)
            return $this->sendError('请勿重复领取');

        if(!$service->draw($user_coupon_info))
            return $this->sendError('领取失败，请稍后重试');

        return $this->sendSuccess();

    }

    /**
     * 优惠券领取
     */
    public function drawCoupons(Request $request, PassengerUserCouponService $service)
    {
        $id = $request->post('id'); //优惠券ID集合
        $user_coupon_info = UserCoupon::query()->where('user_id', Auth::id())->whereIn('id',explode(',',$id))->get();

        if(!$user_coupon_info)return $this->sendError('优惠券不存在');
        foreach ($user_coupon_info as $value)
        {
            if($value['status'] != 0)
                return $this->sendError('请勿重复领取');
        }
        if(!$service->draws($user_coupon_info,Auth::id()))return $this->sendError('领取失败，请稍后重试');

        return $this->sendSuccess();
    }

}
